package com.coder.rental.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coder.rental.entity.Customer;
import com.coder.rental.service.ICustomerService;
import com.coder.rental.utils.Result;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
import cn.hutool.core.date.DateTime;
import com.coder.rental.utils.JwtUtils;
import com.coder.rental.utils.RedisUtils;
import jakarta.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author KB
 * @since 2025-04-15
 */
@RestController
@RequestMapping("/rental/customer")
public class CustomerController {
    @Resource
    private ICustomerService customerService;
    
    @Resource
    private RedisUtils redisUtils;

    @PostMapping("/login")
    public Result login(@RequestParam String username, @RequestParam String password) {
        // 使用MyBatis Plus的lambda查询
        Customer customer = customerService.lambdaQuery()
                .eq(Customer::getUsername, username)
                .one();
        if (customer == null || !customer.getPassword().equals(password)) {
            return Result.fail().setMessage("用户名或密码错误");
        }
        // 生成token
        Map<String, Object> map = new HashMap<>();
        map.put("username", customer.getUsername());
        map.put("userid", customer.getId());
        String token = JwtUtils.createToken(map);
        // 获取token过期时间
        long expireTime = DateTime.now().getTime() + 3600 * 1000; // 1小时过期
        // 将token存入Redis
        String tokenKey = "token:" + token;
        // Remove this line: RedisUtils redisUtils = new RedisUtils();
        redisUtils.set(tokenKey, token, 3600L); // 3600秒 = 1小时
        // 返回响应
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("expireTime", expireTime);
        response.put("id", customer.getId());
        response.put("token", token);
        return Result.success(response).setMessage("登录成功");
    }
    @PostMapping
    public Result save(@RequestBody Customer customer) {
        return customerService.save(customer) ? Result.success(customer.getId()) : Result.fail();
    }
    @PutMapping
    public Result update(@RequestBody Customer customer) {
        return customerService.updateById(customer) ? Result.success() : Result.fail();
    }
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable String ids) {
        return customerService.delete(ids) ? Result.success() : Result.fail();
    }

    @PostMapping("{start}/{size}")
    public Result search(@PathVariable int start, @PathVariable int size,
                         @RequestBody Customer customer){
        Page<Customer> page = new Page<>(start, size);
        return Result.success(customerService.searchByPage(page, customer));
    }
    @PostMapping("selectCustomerByTel")
    public Result selectCustomerByTel(@RequestBody Customer customer){
        return Result.success(customerService.selectByCustomerByTel(customer));
    }
}
